// 1.引入公用样式表
require("../../assets/css/index.less");
// require("../../assets/fonts/iconfont.css")

// 2.引入当前样式表
require("./social.less");

// 引入插件

// 3.引入公用js模块
let validator = require("../../utils/validator.js");
let https = require("../../utils/https.js");
let local = require("../../utils/local.js");
let axios = require("../../utils/axios.js");

// 4.引入工具函数
let dom = require("../../utils/dom.js");
// 判断是否有登录
dom.isToken();
dom.ready(function () {
	// 渲染底部Tab
	dom.render(2);
	// 跳转添加动态
	dom.goTo("#addDynamic", "addDynamic");

	// 获取最新课程
	let newCourseId;
	function getNewCourse() {
		https.get("/api/train/courseList", function (res) {
			console.log(res);
			if (res.errno == 0) {
				let { imgurl, name, desc, courseId } = res.data[0];
				newCourseId = courseId;
				dom.$("#course").innerHTML = `
				<p>推荐课程</p>
				<div class="course-cont">
					<p>
						<img src="${https.BASE_URL + imgurl}" alt="" />
					</p>
					<div>
						<p>${name}</p>
						<p>${desc}</p>
					</div>
				</div>`;
			}
		});
	}
	getNewCourse();
	// 获取省份的函数信息
	async function getProStr(proId, ciId) {
		let result1 = await axios.get(https.BASE_URL + "/api/shared/province");
		let proArr;
		if (result1.data.errno == 0 && result1.data != undefined) {
			proArr = result1.data.data.filter((v) => {
				return v.id == proId;
			});
		} else {
			proArr = [];
		}
		let result2 = await axios.get(
			https.BASE_URL + "/api/shared/city?provinceId=" + proId
		);
		let cityArr;
		if (result2.data.errno == 0 && result2.data != undefined) {
			cityArr = result2.data.data.filter((v) => {
				return v.id == ciId;
			});
		} else {
			cityArr = [];
		}
		return [...proArr, ...cityArr];
	}

	//  渲染朋友圈
	function getNormalMsg() {
		// 发送请求, 获取数据
		https.get("/api/circles/opinionList", function (res) {
			console.log(res.data);
			if (res.errno == 0) {
				//遍历获取
				let topNode = "";
				res.data.forEach((item, i) => {
					// 渲染
					topNode = `
					<div class="dynamic-cont">
						<div class="userInfo df acenter">
							<p id="userAvator"><img src="#" alt="" /></p>
							<p class="df fdc">用户名 <span class="address"></span></p>
						</div>
						<p class="text">${item.content}</p>
						<ul class="imgs df jstart acbetween">`;
					if (item.imgUrls != null && item.imgUrls != []) {
						item.imgUrls.forEach((v) => {
							topNode += `<li><img src="${https.BASE_URL + v}" alt="" /></li>`;
						});
					}
					dom.$("#dynamic").innerHTML += topNode + `</ul></div></div>`;
					// 还是需要有默认值的
					let proId = item.releaseProvinceId || 110000;
					let ciId = item.releaseCityId || 110100;
					// 渲染省市
					getProStr(proId, ciId).then((res) => {
						if (res.length > 1) {
							dom.$(".userInfo .address")[i].innerHTML =
								res[0].name + "," + res[1].name;
						} else if (res.length != 0) {
							dom.$(".userInfo .address")[i].innerHTML = res[0].name;
						}
					});
				});
			}
		});
	}
	getNormalMsg();
	// 跳转课程介绍页
	dom.$("#course").addEventListener("click", function () {
		location.href = `courseIntro.html?id=${newCourseId}`;
	});
});
